package cn.oms.oms.controller;

import cn.oms.oms.service.IUserService;
import cn.oms.oms.vo.LoginRes;
import cn.oms.oms.vo.LoginVo;
import cn.oms.oms.vo.RegisterVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author AllenChen曾晨
 * @since 2022-04-29
 */
@RestController
@RequestMapping("/oms/api")
@Slf4j
public class HomeController {

    @Autowired
    public IUserService userService;

    /**
     * 注册接口
     * @return
     */
    @PostMapping("/register")
    public String registration(@Validated RegisterVo registerVo, BindingResult result){
        log.debug("收到注册信息:{}", registerVo);
        // 判断验证有没有错误
        if (result.hasErrors()) {
            //获得错误信息
            String msg = result.getFieldError().getDefaultMessage();
            //当有验证不通过时,直接返回错误信息到axios
            return msg;
        }
        String res = userService.registerUser(registerVo);

        System.out.println("res = " + res);
        return res;
    }

    /**
     * 登录接口
     * @return
     */
    @PostMapping("/login")
    public LoginRes login(LoginVo loginVo){
        LoginRes loginRes = userService.userLogin(loginVo);
        return loginRes;
    }
}
